/******************************************************************************
 * @File name   :      DealerInfoSyncController.java
 *
 * @Author      :      GUDONG
 *
 * @Date        :      2015年10月14日
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2015年10月14日 下午1:17:16        GUDONG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.dealerinfosync.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cap.bts.crmdp.dealer.dealerinfosync.domain.DealerInfoRelMars;
import com.cap.bts.crmdp.dealer.dealerinfosync.service.IDealerInfoSyncService;
import com.cap.bts.crmdp.dealer.dealerinfosync.util.ProcessCsvFileUtils;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Controller
@RequestMapping(value = "/crmdp/dealer/dealerinfosync")
public class DealerInfoSyncController extends BaseController {
    @Autowired
    IDealerInfoSyncService dealerInfoSyncService;
    
    @SuppressWarnings("rawtypes")
    public void saveFile(List<Map<String, Object>> dataList) throws AppException {
        dealerInfoSyncService.deleteDealerInfoSync();
        for(int i=0;i<dataList.size();i++){
            Map<String, Object> raw = dataList.get(i);
            DealerInfoRelMars dealerInfoRelMars = (DealerInfoRelMars) ProcessCsvFileUtils.convertMap(DealerInfoRelMars.class, raw);
            dealerInfoSyncService.saveDealerInfoSync(dealerInfoRelMars);
        }
    }
    
    @RequestMapping(value = "/saveFilePath")
    public void saveFilePath() throws AppException {
        File file = new File("C:/Users/gudong/Desktop/TenantServiceCenter.csv");
        List<Map<String, Object>> dataList = ProcessCsvFileUtils.combinationList(ProcessCsvFileUtils.analyzeCsv(file));
        for(int i=0;i<dataList.size();i++){
            Map<String, Object> raw = dataList.get(i);
            DealerInfoRelMars dealerInfoRelMars = new DealerInfoRelMars();
            dealerInfoRelMars.setCompany(raw.get("company").toString());
            dealerInfoRelMars.setDealerId(Integer.parseInt(raw.get("dealerId").toString()));
            dealerInfoRelMars.setMarsDealerId(Integer.parseInt(raw.get("dealerId").toString()));
            dealerInfoRelMars.setMarsTenantId(raw.get("tenantID").toString());
            dealerInfoSyncService.saveDealerInfoSync(dealerInfoRelMars);
        }
    }
}
